package com.longy.gulimall.product.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
@RequestMapping("/api/logs2")
public class LogLevelController2 {

    private RestTemplate restTemplate;

    @Autowired
    public void LoggingController(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }

    @PostMapping("/set-level")
    public ResponseEntity<String> setLogLevel(@RequestParam String loggerName, @RequestParam String level) {
        String url = "/actuator/loggers/" + loggerName;
        String body = "{\"configuredLevel\":\"" + level + "\"}";

        HttpEntity<String> request = new HttpEntity<>(body);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

        return response;
    }

    @GetMapping("/testLog")
    public String testLog() {
        log.debug("这是一个debug日志...");
        log.info("这是一个info日志...");
        log.error("这是一个error日志...");
        return "test log";
    }
}
